from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from .serializers import StudentsSerializer

from students.models import Students
# Create your views here.


class StudetntsView(View):

    @classmethod
    def get(self, request):
        data_dict = {"name": "小明", "age": 12}
        return JsonResponse(data_dict)


class StudentsAPIView(GenericAPIView):
    queryset = Students.studentsmanger.all()
    serializer_class = StudentsSerializer

    def get(self, request, pk):
        data = self.get_object()
        serializer = StudentsSerializer(instance=data)
        return Response(serializer.data)

    def post(self, request):
        serializer = StudentsSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data)
